#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
@File    :   test_home.py
@Time    :   2024/12/10 00:23:04
@Author  :   xiangyu Chen
@Version :   1.0
@Contact :   yu112yu@sina.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

from time import sleep

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from utils.img_utlis import ocr_verify_code

# 打开参数,这个参数是实验性参数
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation', '--headless', '--no-sandbox', '--disable-dev-shm-usage', '-- ignore -certificate-errors'])
option.page_load_strategy = 'none'  # 设置 pageLoadStrategy 为 "none" (也就是非阻塞:get)
#get直接返回，不再等待界面加载完成
# desired_capabilities = DesiredCapabilities.CHROME
# desired_capabilities["pageLoadStrategy"] = "none"

# 指定chromedriver, 此处使用了相对路径
service = Service('./webdriver/chromedriver_131.0.6778.85_x32.exe')
driver = webdriver.Chrome(service=service, options=option)
# driver.maximize_window()
# driver.set_page_load_timeout(10)  #把网页加载的最大时间设置为 0.01s，这样，get 方法立刻就会抛出 TimeoutException，我们不管它，然后用 WebDriverWait 判断网页有没有加载完毕。
# 设置脚本执行超时时间为5秒
driver.set_script_timeout(10)
# 隐式等待：最长等待5秒钟往下执行
driver.implicitly_wait(10)

test_url = "http://local.shop.com/index.php/Home/index/index.html"
try:
    print("get_url: ", test_url)
    driver.get(test_url)
    home_h = driver.current_window_handle  #获取当前页句柄，get后马上获取即为首页
    handles = driver.window_handles  #获取所有句柄
    # driver.switch_to.window(handles[0])
    for handle in handles:
        driver.switch_to.window(handle)  #按句柄切换页面
        print('driver.current_url: ', driver.current_url)
except TimeoutException:
    # 10s是最长等待时间，0.5是每0.5秒去查询对应的元素。until后面跟的等待具体条件，EC是判断条件，检查元素是否存在于页面的 DOM 上。
    # login_btn = WebDriverWait(driver, 10, 0.5).until(lambda driver: driver.find_element(By.LINK_TEXT, "登录"))
    print('TimeoutError')
finally:
    login_btn = WebDriverWait(driver, 10, 0.2).until(EC.presence_of_element_located((By.LINK_TEXT, "登录")))

    # 点击进入登录页
    login_btn.click()

    username_text = WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, "username")))
    username_text.clear()
    username_text.send_keys("18900000001")

    password_text = WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.ID, "password")))
    password_text.clear()
    password_text.send_keys("123456")

    verify_text = WebDriverWait(driver, 10, 0.5).until(lambda driver: driver.find_element(By.NAME, 'verify_code'))

    verify_img = WebDriverWait(driver, 10, 0.5).until(lambda driver: driver.find_element(By.ID, 'verify_code_img'))
    verify_code = ocr_verify_code(verify_img.screenshot_as_png, 4)

    verify_text.clear()
    verify_text.send_keys(verify_code)
    # # 点击登录网站
    driver.find_element(By.CLASS_NAME, 'J-login-submit').click()
    print('输入验证码登录步骤完成！')

    WebDriverWait(driver, 10, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, "mu-m-phone")))
    print('成功登录！')
    print('mine_present_url: ', driver.current_url)

    # 6. 暂停
    sleep(5)

    # 7. 关闭浏览器
    driver.quit()
    
